package org.backend.canteenbackend.service.impl;


import org.backend.canteenbackend.entity.Account;
import org.backend.canteenbackend.entity.Profile;
import org.backend.canteenbackend.persistence.AccountMapper;
import org.backend.canteenbackend.persistence.ProfileMapper;
import org.backend.canteenbackend.service.GiteeOAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service("giteeOAuthService")
public class GiteeOAuthServiceImpl implements GiteeOAuthService {
    private final String CLIENT_ID = "c56e9f38cfb90df69c93970e2b3d01f04e821fa19ebf1d8774b93ebd15a5d014";
    private final String CLIENT_SECRET = "d8ed02af5edba99a2b00b499392d95a09873ce9d911f00f0293b20a909287513";
    private final String REDIRECT_URI = "http://localhost:8088/auth/gitee";
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ProfileMapper profileMapper;
    public String getGiteeUsername(String code) {
        RestTemplate restTemplate = new RestTemplate();

        // 交换 Token
        String accessTokenUrl = "https://gitee.com/oauth/token"
                + "?client_id=" + CLIENT_ID
                + "&client_secret=" + CLIENT_SECRET
                + "&redirect_uri=" + REDIRECT_URI
                + "&grant_type=" + "authorization_code"
                + "&code=" + code;

        Map<String, String> response = restTemplate.postForObject(accessTokenUrl, null, HashMap.class);
        String accessToken = response.get("access_token");

        // 获取 GitHub 用户信息
        String userInfoUrl = "https://gitee.com/api/v5/user";
        Map<String, Object> userResponse = restTemplate.getForObject(userInfoUrl + "?access_token=" + accessToken, Map.class);
        Account account=accountMapper.selectById((String) userResponse.get("login"));
        if(account==null){
            insertIntoInformation((String) userResponse.get("login"));
        }
        return (String) userResponse.get("login");
    }
    private boolean insertIntoInformation(String username) {

        Account account=new Account();
        account.setUsername(username);
        account.setEmail("请完善你的信息！");
        account.setFirstName("请完善你的信息！");
        account.setLastName("请完善你的信息！");
        account.setStatus("OK");
        account.setAddress1("请完善你的信息！");
        account.setAddress2("请完善你的信息！");
        account.setCity("请完善你的信息！");
        account.setState("请完善你的信息！");
        account.setZip("请完善你的信息！");
        account.setCountry("请完善你的信息！");
        account.setPhone("请完善你的信息！");

        Profile profile=new Profile();
        profile.setUsername(username);
        profile.setBanneropt(0);
        profile.setFavcategory("请完善你的信息！");
        profile.setLangpref("请完善你的信息！");
        profile.setMylistopt(0);
        int result1=accountMapper.insert(account);
        int result2=profileMapper.insert(profile);
        if(result1>0&&result2>0){
            return true;
        }
        return false;
    }
}


